#include <QFile>
#include <iostream>
#include <QDebug>
#include "TsFileReader.h"

TsFileReader::TsFileReader()
{
}

bool TsFileReader::readFile(const QString &fileName)
{
    QFile file(fileName);
    if (!file.open(QFile::ReadOnly | QFile::Text)){
        std::cerr << "Error: Cannot read file " << qPrintable(fileName)
        << ": " << qPrintable(file.errorString())
        << std::endl;
        return false;
    }
    qDebug() << QString("read success");
    reader.setDevice(&file);
    reader.readNext();
    while (!reader.atEnd()){
        if (reader.isStartElement()){
            if (reader.name() == "TS"){
                qDebug() << reader.attributes().value("version").toString();
                qDebug() << reader.attributes().value("language").toString();
                qDebug() << reader.attributes().value("sourcelanguage").toString();
                //break;
                readTsElement();
            } else {
                reader.raiseError(QObject::tr("Not a ts file"));
            }
        } else {
            reader.readNext();
        }
    }

    file.close();

    if (reader.hasError()){
        std::cerr << "Error: Failed to parse file "
        << qPrintable(fileName) << ": "
        << qPrintable(reader.errorString()) << std::endl;
        return false;
    } else if (file.error() != QFile::NoError){
        std::cerr << "Error: Cannot read file " << qPrintable(fileName)
        << ": " << qPrintable(file.errorString())
        << std::endl;
        return false;
    }
    return true;
}

void TsFileReader::readTsElement()
{
    reader.readNext();
    while (!reader.atEnd()){
        if (reader.isEndElement()){
            reader.readNext();
            break;
        }
        if (reader.isStartElement()){
            if (reader.name() == "context"){
                readContexElement();
            } else {
                skipUnknownElement();
            }
        } else {
            reader.readNext();
        }
    }
}

void TsFileReader::readContexElement()
{
    reader.readNext();
    while (!reader.atEnd()){
        if (reader.isEndElement()){
            reader.readNext();
            break;
        }
        if (reader.isStartElement()){
            if (reader.name() == "name"){
                readNameElement();
            } else if(reader.name() == "message") {
                readMessageElement();
            } else {
                skipUnknownElement();
            }
        } else {
            reader.readNext();
        }
    }
}

void TsFileReader::readNameElement()
{
    QString name = reader.readElementText();
    qDebug() << name;
    if (reader.isEndElement())
        reader.readNext();
}

void TsFileReader::readMessageElement()
{
    reader.readNext();
    while (!reader.atEnd()){
        if (reader.isEndElement()){
            reader.readNext();
            break;
        }
        if (reader.isStartElement()){
            if (reader.name() == "location"){
                readLocationElement();
            } else if(reader.name() == "source") {
                readSourceElement();
            } else if(reader.name() == "translation") {
                readTranslationElement();
            } else {
                skipUnknownElement();
            }
        } else {
            reader.readNext();
        }
    }
}

void TsFileReader::readLocationElement()
{
    qDebug() << reader.attributes().value("filename").toString();
    qDebug() << reader.attributes().value("line").toString();
    reader.readElementText();

    if (reader.isEndElement())
        reader.readNext();
}

void TsFileReader::readSourceElement()
{
    QString name = reader.readElementText();
    qDebug() << name;
    if (reader.isEndElement())
        reader.readNext();
}

void TsFileReader::readTranslationElement()
{
    qDebug() << reader.attributes().value("type").toString();
    QString name = reader.readElementText();
    qDebug() << name;
    if (reader.isEndElement())
        reader.readNext();
}

void TsFileReader::skipUnknownElement()
{
    reader.readNext();
    while (!reader.atEnd()){
        if (reader.isEndElement()){
            reader.readNext();
            break;
        }
        if (reader.isStartElement()){
            skipUnknownElement();
        } else {
            reader.readNext();
        }
    }
}
